package avicit.bdp.dds.remote.utils;

import avicit.bdp.core.constant.Constants;
import com.alibaba.fastjson2.JSON;

/**
 *  json serialize or deserialize
 */
public class FastJsonSerializer {

	/**
	 * serialize to byte
	 *
	 * @param obj object
	 * @param <T> object type
	 * @return byte array
	 */
	public static <T> byte[] serialize(T obj)  {
		String json = JSON.toJSONString(obj);
		return json.getBytes(Constants.UTF8);
	}

	/**
	 *  serialize to string
	 * @param obj object
	 * @param <T> object type
	 * @return string
	 */
	public static <T> String serializeToString(T obj)  {
		return JSON.toJSONString(obj);
	}

	/**
	 *  deserialize
	 *
	 * @param src byte array
	 * @param clazz class
	 * @param <T> deserialize type
	 * @return deserialize type
	 */
	public static <T> T deserialize(byte[] src, Class<T> clazz) {
		return JSON.parseObject(src, clazz);
	}

}
